<?php
/**
 * 功能：card_group
 * 
 * 
 */
class GroupAction extends CommonAction {
    public function index(){
        layout('Layout/common'); //布局文件Tpl/Layout/common.html

        $login_id = $this->login_id; //当前登录用户ID
        
        //分组
        $_group = M('Group');        
        $where_group = "uid='{$login_id}'";
        $group_data = $_group->where($where_group)->select();
        $group_list = array();
        foreach ($group_data as $k=>$v) {
            $group_list[$v['gid']] = $v['group_name'];
        }
        
        $this->assign('group_list',$group_data);
        $this->display();
    }

    //插入
    public function insert(){
        $login_id = $this->login_id; //当前登录用户ID

        $name = $this->getActionName();
        $model = D($name);

        $where = "uid='{$login_id}'";
        $num = $model->where($where)->count();
        if($num >= 10){
            $this->error('创建分组超过上限10!');
        }

        $group_name = $this->_post("group_name","trim");

        $data['group_name'] = $group_name;
        $data['uid'] = $login_id;
        $data['create_time'] = time();
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        $id = $model->add($data);
        if ($id !== false) { //保存成功
            $list = $model->where($where)->field('gid,group_name,uid,create_time')->order('gid asc')->select();
            $menu = empty($list) ? array() : $list;
            //缓存菜单访问
            $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]] = $menu;

            $this->success('新增成功!',"/group/index");
        } else {
            //失败提示
            $this->error('新增失败!');
        }
    }

    //编辑页面
    function edit() {
        $uid = $this->login_id; //登录用户ID

        $name = $this->getActionName();
        $model = M($name);
        //$id = $_REQUEST [$model->getPk()];
        $gid = $_REQUEST ['id'];
        $vo = $model->getByGid($gid);

        $this->assign('vo', $vo);
        $this->display();
    }

    //更新
    function update() {
        $gid = $this->_param('gid','',0);
        $group_name = $this->_param('group_name',"trim");

        $name = $this->getActionName();
        $model = D($name);

        $where = "gid = '{$gid}'";
        $data['group_name'] = $group_name;

        if (false === $model->create()) {
            $this->error($model->getError());
        }
        // 更新数据
        $nid = $model->where($where)->save($data);
        if (false !== $nid) {
            $where_list = "uid='{$this->login_id}'";
            $list = $model->where($where_list)->field('gid,group_name,uid,create_time')->order('gid asc')->select();
            $menu = empty($list) ? array() : $list;
            //缓存菜单访问
            $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]] = $menu;

            $this->success('编辑成功!',cookie('_currentUrl_'));
        } else {           
            $this->error('编辑失败!');
        }
    }

    //根据条件禁用/启用表数据: flag [1启用 0禁用]
    public function forbid(){
        $mid = $this->_get('id','',0);
        $flag = $this->_get('flag','',0);

        $options = "mid='{$mid}'";
        $field = "g_state";
        $value = ($flag == 1) ? 1 : 0;
        $msg = ($flag == 1) ? '启用' : '禁用';

        $model = M('Info_group');
        if(FALSE === $model->where($options)->setField($field,$value)){
            $this->error =  L('_OPERATION_WRONG_');
            //return false;
            $this->success($msg.'失败!');
        }else {
            //return True;
            $this->success($msg.'成功!',"/info/index");
        }
    }


    

    
}